package com.ybkj.daijia.common;

import java.io.IOException;
import java.io.InputStream;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.multipart.MultipartFile;


/**
 * <p>
 * Copyright 2013 成都易米网络科技有限公司. All Rights Reserved
 * <ul>.
 * <li>网址: <a href="http://www.easymin.com">http://www.easymin.com</a></li>
 * <li>电话: 028-61703886</li>
 * </ul>
 * </p>
 * <p>
 * 判断是否为图片
 * </p>
 *
 * @author FZH
 * @version 1.0.0
 */
public final class MediaUtil {

    /**
     * 工具类不需要实例化.
     */
    private MediaUtil() {
    }

    /**
     * 判断是否为图片.
     *
     * @param ext 判断标识 如只传bmp则验证是否为bmp格式的图片，否则就验证是否为图片
     * @return true是图片, false不是图片
     */
    public static boolean isPicture(MultipartFile image, final String ext) throws IOException {
        String fileName = image.getOriginalFilename();

        if (StringUtils.isBlank(fileName)) {
            return false;
        }
        // 获得文件后缀名
        String tmpName = fileName
            .substring(StringUtils.lastIndexOf(fileName, ".") + 1, fileName.length()).toLowerCase();
        // 声明图片后缀名数组
        String[][] extArray = {
            {"bmp", "0"},
            {"gif", "1"},
            {"jpeg", "2"},
            {"jpg", "3"},
            {"png", "4"}
        };

        for (int i = 0; i < extArray.length; i++) {
            // 判断单个类型文件
            if (!StringUtils.isBlank(ext) && extArray[i][0].equals(tmpName) && extArray[i][1]
                .equals(ext)) {
                break;
            }
            // 判断符合全部类型
            if (StringUtils.isBlank(ext) && extArray[i][0].equals(tmpName)) {
                break;
            }
            if (i == extArray.length - 1) {
                return false;
            }
        }

        if (getPicType(image.getInputStream()).equals("unknow")) {
            return false;
        }

        return true;
    }

    public static String bytesToHexString(byte[] src) {
        StringBuilder stringBuilder = new StringBuilder();
        if (src == null || src.length <= 0) {
            return null;
        }
        for (int i = 0; i < src.length; i++) {
            int v = src[i] & 0xFF;
            String hv = Integer.toHexString(v);
            if (hv.length() < 2) {
                stringBuilder.append(0);
            }
            stringBuilder.append(hv);
        }
        return stringBuilder.toString();
    }

    public static String getPicType(InputStream fis) {
        //读取文件的前几个字节来判断图片格式
        byte[] b = new byte[4];
        try {
            fis.read(b, 0, b.length);
            String type = bytesToHexString(b).toUpperCase();
            if (type.contains("FFD8FF")) {
                return "jpg";
            } else if (type.contains("89504E47")) {
                return "png";
            } else if (type.contains("47494638")) {
                return "gif";
            } else if (type.contains("424D")) {
                return "bmp";
            } else {
                return "unknow";
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (fis != null) {
                try {
                    fis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return null;
    }
}
